import { defineStore } from "pinia";
import { getUserInfoApi } from "@/api";
import menuData, { adminMenus } from "@/assets/ts/menuData";
import type { ThemeConfig } from "ant-design-vue/es/config-provider/context";
import type { MenuTheme } from "ant-design-vue";
export type UserInfo = {
    id: number;
    level: number;
};
export default defineStore("main", () => {
    const author = ref("");
    const username = ref("");
    const userInfo = ref<Record<string, any>>({});
    const getUserInfo = () => {
        return new Promise((resolve) => {
            getUserInfoApi(username.value).then((res) => {
                if (res.data.code === 200) {
                    userInfo.value = res.data.data;
                    resolve(null);
                } else {
                    resolve(null);
                }
            });
        });
    };
    // 网站列表表格字段
    const websitesTableFields = ref<Array<string>>([""]);
    const newMenuData = ref<Array<MenuDataItem>>([]);
    const isAdmin = computed(() => userInfo.value && userInfo.value.level === 1);
    const setNewMenuData = () => {
        // 验证是否为管理员
        if (isAdmin.value) {
            newMenuData.value = [adminMenus, ...menuData];
        } else {
            newMenuData.value = menuData;
        }
    };
    const theme = ref<MenuTheme>("dark")
    // isAdmin
    const menuKeys = computed<Array<string>>(() => {
        const arr = [] as Array<string>;
        const deepFunc = function (menuData: Array<MenuDataItem>) {
            for (const menu of menuData) {
                if (menu.children) {
                    deepFunc(menu.children);
                } else {
                    arr.push(menu.key);
                }
            }
        };
        deepFunc(menuData);
        return arr;
    });

    const themeConfig:ThemeConfig = reactive({
        token:{
            colorPrimary:"#3b5999"
        }
    })
    watch(theme,()=>{
        document.documentElement.setAttribute('data-theme',theme.value)

    },{immediate:true})
    return {
        author,
        username,
        userInfo,
        menuData: newMenuData,
        menuKeys,
        getUserInfo,
        setNewMenuData,
        websitesTableFields,
        themeConfig,
        theme
    };
});